from config import  get_config_from_xml
import os
# from train import training_main
# from data_proc.prepare_dataset import prepare_dataset
# from evaluate import evaluate
# #五个损失函数
# from models.R2_UNet import R2_UNet
# from models.UNet import UNet
# from models.Res_UNet import ResUNet
# from models.Attn_UNet import Attn_UNet
#
# from loss_fn.Jaccard_loss import JaccardLoss
# from loss_fn.Tversky_loss import TverskyLoss
# from loss_fn.Combined_Loss import CombinedLoss
# from segmentation_models_pytorch.losses import DiceLoss
from torch.nn import BCEWithLogitsLoss
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor

from data_proc.dataset_prepare import prepare_dataset
from models.FastRCNN import FastRCNN_model#这个虽然提示没被调用，实际上隐含被调用不能删除
from train import training_main
from evaluate_image import evaluate_image
from evaluate_video import evaluate_video
# 四个模型
def UNOdetect_main(config_file):
    # 检查配置文件是否存在
    if not os.path.exists(config_file):
        print(f"错误：配置文件 '{config_file}' 不存在。")
        return  # 停止执行后续代码

    cfg = get_config_from_xml(config_file)
    #准备数据划分
    prepare_dataset(cfg)
    model_name = cfg['MODEL_NAME']
    # loss_name =  cfg['LOSS_FXN']

    # 实例化模型,将纯字符,转换成模型函数的方法，即文字映射到函数上
    model = globals()[model_name](cfg['N_CLASSES']).to(cfg['DEVICE'])

    #训练函数
    training_main(cfg, model)#如果想体验训练过程，就一个流程走下来
    # #预测函数
    evaluate_image(cfg)#evaluate_image.py 也可以单独运行这个文件 必定作业不需要训练
    evaluate_video(cfg)#evaluate_video.py 也可以单独运行这个文件



if "__main__" == __name__:
    config_file = 'configurations/config_fastrcnn_bce_epoch_10.xml'
    UNOdetect_main(config_file)


